package com.fenghongzhang.day016;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.RemoteViews;

import java.io.IOException;
import java.util.List;
import java.util.Random;

public class MusicService extends Service {

    private MediaPlayer mediaPlayer;
    private List<Music> list;
    private int index = 0;
    private int playModel = 0;


    public List<Music> getList() {
        return list;
    }

    public void setList(List<Music> list) {
        this.list = list;
    }

    public MusicService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();
        list = MusicUtil.getData(this);

        //注册广播接收者的频道
        MyBroadcast myBroadcast = new MyBroadcast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.feng.pause");
        intentFilter.addAction("com.feng.restart");
        registerReceiver(myBroadcast,intentFilter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //前台服务
        serviceNotify(0);
        return super.onStartCommand(intent, flags, startId);
    }

    class MyBinder extends Binder{
//        public void callPlay(int position){
//            play(position);
//        }
//        public void callPause(){
//      返回当前的服务对象
        public MusicService getService(){
            return MusicService.this;
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    public void play(int position){
        //发送通知前台服务
        //前台服务
        serviceNotify(position);

        index = position;
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(list.get(position).getData());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

        //播放完成监听器
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                switch (playModel){
                    case MusicUtil.ORDER:
                        next();
                        break;
                    case MusicUtil.SINGLE:
                        play(index);
                        break;
                    case MusicUtil.RANDOM:
                        index = new Random().nextInt(list.size());
                        play(index);
                        break;
                }
            }
        });

    }

    private void serviceNotify(int i) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("标题");

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);

        //相当于发送一个广播. sendbroadcast
        Intent intent1 = new Intent();
        intent1.setAction("com.feng.pause");
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, intent1, 0);
        remoteViews.setOnClickPendingIntent(R.id.notify_pause, broadcast);


        Intent intent2 = new Intent();
        intent2.setAction("com.feng.restart");
        PendingIntent broadcast2 = PendingIntent.getBroadcast(this, 0, intent2, 0);
        remoteViews.setOnClickPendingIntent(R.id.notify_restart, broadcast2);

        remoteViews.setTextViewText(R.id.notify_title,list.get(i).getTitle());

        //设置封面
        remoteViews.setImageViewBitmap(R.id.notify_img,
                BitmapFactory.decodeFile(MusicUtil.getPic(list.get(i).getAlbumId(),this)));

        builder.setCustomContentView(remoteViews);

        //发送
        startForeground(1, builder.build());
    }


    public int getMax(){
        return mediaPlayer.getDuration();
    }

    public int getCurrPosition(){
        return mediaPlayer.getCurrentPosition();
    }


    public void setProgress(int progress){
        mediaPlayer.seekTo(progress);
    }

    public void pause(){
        mediaPlayer.pause();
    }

    public void stop(){
        mediaPlayer.stop();
    }
    public void restart(){
        mediaPlayer.start();
    }

    public void next(){
        if(++index > list.size()-1){
            index = 0;
        }
        play(index);
    }

    public void pre(){
        if(--index <= 0){
            index = 0;
        }
        play(index);
    }

    //设置播放模式
    public void setPlayModel(int playModel) {
        this.playModel = playModel;
    }

    class MyBroadcast extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals("com.feng.pause")){
                pause();
            }else if(action.equals("com.feng.restart")){
                restart();
            }

        }
    }
}
